package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/tuokeActivity/model"
	"strconv"
)

type TuokeActivityController struct {
	Activity *model.Activity
}

func (tac *TuokeActivityController)SharedOrders(c *fiber.Ctx){

	tac.Activity = new(model.Activity)

	shareRecordsStr := c.FormValue("shareRecords")
	var shareRecords model.ShareRecords
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(shareRecordsStr), &shareRecords)

	res , err := tac.Activity.SharedOrders(&shareRecords)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (tac *TuokeActivityController)EventPurchase(c *fiber.Ctx){

	tac.Activity = new(model.Activity)

	activityIdStr := c.FormValue("activityId")
	activityId, err := strconv.ParseInt(activityIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := tac.Activity.EventPurchase(userId, activityId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (tac *TuokeActivityController)ParticipationActivity(c *fiber.Ctx){

	tac.Activity = new(model.Activity)

	activityIdStr := c.FormValue("activityId")
	activityId, err := strconv.ParseInt(activityIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := tac.Activity.ParticipationActivity(activityId, userId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (tac *TuokeActivityController)QueryActivity(c *fiber.Ctx){

	tac.Activity = new(model.Activity)

	res , err := tac.Activity.QueryActivity()
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (tac *TuokeActivityController)ExchangePrizes(c *fiber.Ctx){

	tac.Activity = new(model.Activity)

	activityIdStr := c.FormValue("activityId")
	activityId, err := strconv.ParseInt(activityIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	medalManagementIdStr := c.FormValue("medalManagementId")
	medalManagementId, err := strconv.ParseInt(medalManagementIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := tac.Activity.ExchangePrizes(userId, medalManagementId, activityId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}




















